﻿#include "CLabelIr.h"
#include "CHeader.h"

CLabelIr::CLabelIr(QWidget *parent)
{
	setParent(parent);
	m_sFileName = "";
	m_bPressed = false;
	m_sFilePath = "";
	m_dMaxTemp = 50;
	m_dMinTemp = 20;
}

CLabelIr::~CLabelIr()
{
}



void CLabelIr::InitUi(QString sFilePath)
{
	m_sFileName = sFilePath;
	m_pPixmapA = new QPixmap(m_sFileName);
	GetMyTempPix();
}

void CLabelIr::mouseMoveEvent(QMouseEvent * event)
{
	QPoint point = event->pos();
	m_pPointB = point;
	GetRectTemp(m_pPointA, m_pPointB);
	update();
	return QWidget::mouseMoveEvent(event);
}

void CLabelIr::mousePressEvent(QMouseEvent * event)
{
	if (event->button() == Qt::LeftButton)
	{
		QPoint point = event->pos();
		m_pPointA = point;

	}
	return QWidget::mousePressEvent(event);
}

void CLabelIr::mouseReleaseEvent(QMouseEvent * event)
{
	if (event->button() == Qt::LeftButton)
	{
		QPoint point = event->pos();
		m_pPointB = point;
		GetRectTemp(m_pPointA, m_pPointB);
		update();
	}
	return QWidget::mousePressEvent(event);
}

void CLabelIr::GetMyTempPix()
{
	m_dMaxTemp = -10;
	m_dMinTemp = 100;
	QStringList sList = m_sFileName.split('.');
	if (sList.size() <= 0)
		return;
	QString sFileDataName = sList[0] + ".data";
	QByteArray bData = sFileDataName.toLatin1();
	char *cStr = bData.data();
	FILE* datafilefp = fopen(cStr, "rb+");
	if (NULL == datafilefp)
	{
		printf("Failed to open the file\n");
		return;
	}

	int m_AnalysisHotPic_W = PICWIDTH; //根据抓热图接口返回的图像宽dwJpegPicWidth进行赋值
	int m_AnalysisHotPic_H = PIGHEIGHT; //根据抓热图接口返回的图像高dwJpegPicHeight进行赋值
	int bufSize = m_AnalysisHotPic_H * m_AnalysisHotPic_W * 4;
	int iIndex = 0;
	char* pDataBuf = (char*)malloc(bufSize);
	if (NULL == pDataBuf)
	{
		return;
	}
	//读取文件里面所有数据
	fread(pDataBuf, bufSize, 1, datafilefp);
	int j = 0;
	for (int iWriteHeight = 0; iWriteHeight < m_AnalysisHotPic_H; ++iWriteHeight)
	{
		for (int iWriteWidth = 0; iWriteWidth < m_AnalysisHotPic_W; ++iWriteWidth)
		{
			float fTemp = *((float*)(pDataBuf + iIndex));
			if (fTemp > m_dMaxTemp)
			{
				m_dMaxTemp = fTemp;
			}
			
			if (fTemp < m_dMinTemp)
			{
				m_dMinTemp = fTemp;
			}
			if (iIndex < bufSize)
			{
				m_mapMyTempPix.insert(j, fTemp);
				iIndex += 4;
			}
			j++;
		}
	}
}

void CLabelIr::GetRectTemp(QPoint pA, QPoint pB)
{
	int nX = qMin(pA.x(), pB.x());
	int nY = qMin(pA.y(), pB.y());
	int nHeightB = abs(pB.y() - pA.y());
	int nWidthB = abs(pB.x() - pA.x());
	int nWidth = width();
	int nHeight = height();
	float dMaxTemp = -20;
	float dMinTemp = 100;
	float dTempB = 0;
	double dXscale = ((double)nWidth) / PICWIDTH;
	double dYscale = ((double)nHeight) / PIGHEIGHT;
	for (int i = 0; i < nWidthB; i++)
	{
		for (int j = 0; j < nHeightB; j++)
		{
			int nPicX = (nX+i)/ dXscale;
			int nPicY = (nY+j)/ dYscale;
			int nIndex = nPicX+(nPicY-1) * PICWIDTH;

			dTempB = m_mapMyTempPix[nIndex];
			if (dTempB >= dMaxTemp)
			{
				dMaxTemp = dTempB;
				m_cPointMax.setX(nX + i);
				m_cPointMax.setY(nY + j);
				m_dMaxTempOn = dMaxTemp;
			}
			if (dTempB <= dMinTemp)
			{
				dMinTemp = dTempB;
				m_cPointMin.setX(nX + i);
				m_cPointMin.setY(nY + j);
				m_dMinTempOn = dMinTemp;
			}
		}
	}
	m_cRectChose.setRect(nX, nY, nWidthB, nHeightB);
}

void CLabelIr::paintEvent(QPaintEvent *)
{
	QImage* img = new QImage;
	QPainter painter(this);
	painter.drawPixmap(0, 0, width(), height(), *m_pPixmapA);
	painter.setPen(QColor(85, 255, 0));
	painter.drawRect(m_cPointMax.x()-10, m_cPointMax.y()-10, 20, 20);

	painter.setPen(QColor(85, 255, 0));
	painter.drawRect(m_cPointMin.x()-10, m_cPointMin.y()-10, 20, 20);
	painter.drawRect(m_cRectChose);
	QRect textRect = QRect(m_cPointMax.x(), m_cPointMax.y(), 100, 10);
	QFont f = QFont("Microsoft YaHei", 8, QFont::Bold);
	painter.setFont(f);
	painter.setPen(QColor(85,255,0));
	QString sStr = A2T("最高温度:") + QString::number(m_dMaxTempOn);
	painter.drawText(textRect, Qt::AlignCenter, sStr);
	QRect textRectMin = QRect(m_cPointMin.x(), m_cPointMin.y(), 100, 10);
	painter.setFont(f);
	painter.setPen(QColor(85, 255, 0));
	QString sStrMin = A2T("最低温度:") + QString::number(m_dMinTempOn);
	painter.drawText(textRectMin, Qt::AlignCenter, sStrMin);
}
